﻿
@{
    ViewBag.Title = "店铺列表";
}

@section CSS{

}
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            店铺
            <small>列表</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li><a href="#">商家管理</a></li>
            <li class="active">店铺列表</li>
        </ol>
    </section>
    <section>
        <div class="col-xs-12 ">
            <div class="box box-primary">
                <div class="box-body">
                    <div class="col-md-6 ">
                        <div class="info-box-content">
                            <span class="info-box-number"><small>总店铺： </small>@ViewBag.user_total  </span>
                        </div>
                    </div>
                    <div class="col-md-3 ">
                        <div style="text-align:center;">
                            <span class="info-box-number">@ViewBag.user_today </span>
                            <span class="info-box-text" style="color:#c2c1c1;">今日新增店铺</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-xs-12">
                <div class="box">
                    <div class="box-header my_search">
                        <select style="width:120px;" class="form-control pull-left" id="classSelect">
                            <option value="-1">-店铺状态-</option>
                            <option value="1">启用</option>
                            <option value="0">禁用</option>
                        </select>
                        <input type="text" style="width:200px;" class="form-control pull-left" id="shopName" placeholder="店铺名称">
                        <input type="text" style="width:120px;" class="form-control pull-left" id="managerName" placeholder="联系人姓名">
                        <input type="text" style="width:120px;" class="form-control pull-left" id="startTime" placeholder="添加时间-起">
                        <span style="line-height:30px;">至</span>
                        <input type="text" style="width:120px;" class="form-control pull-left" id="endTime" placeholder="添加时间-止">
                        <button type="button" class="btn btn-default " id="btnSearch"><i class="fa fa-search"></i> 查询</button>
                    </div>
                    <!-- /.box-header -->
                    <div class="box-body">
                        <table id="DataList" class="table table-bordered table-striped table-hover ">
                            <thead>
                                <tr>
                                    <th>店铺编号</th>
                                    <th>店铺名称</th>
                                    <th>所属商家</th>
                                    <th>联系人</th>
                                    <th>开店时间</th>
                                    <th>账期(天)</th>
                                    <th>添加时间</th>
                                    <th>店铺状态</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody> </tbody>
                        </table>
                    </div>
                    <!-- /.box-body -->
                </div>
                <!-- /.box -->
            </div>
            <!-- /.col -->
        </div>
        <!-- /.row -->
    </section>
    <!-- /.content -->
</div>
<!-- /.content-wrapper -->
@section JS{
    <script>

        $(function () {
            //初始化表格
            GenerateTable();
            //鼠标移动到该行变色
            $('.table-hover tbody').on('click', 'tr', function () {
                if ($(this).hasClass('selected')) {
                    $(this).removeClass('selected');
                    console.log();
                }
                else {
                    u_table.$('tr.selected').removeClass('selected');
                    $(this).addClass('selected');
                }
            });
            //搜索事件
            $("#btnSearch").on("click", function () {
                var classSelect = $("#classSelect").val();
                var datemin = $('#startTime').val();
                var datemax = $("#endTime").val();
                if ((datemin == null && datemax != null) || (datemin != null && datemax == null)) {
                    layer.msg('请把时间填充完整!', { icon: 1, time: 1000 });
                }
                else {
                    u_table.fnDestroy();
                    GenerateTable();
                }
            });

            $('#startTime').datepicker({
                autoclose: true
            });

            $('#endTime').datepicker({
                autoclose: true
            });
        });
        //生成表格
        function GenerateTable() {
            u_table = $('#DataList').dataTable({
                "lengthChange": true,
                "searching": false,
                "ordering": false,
                "iDisplayLength": 10,
                "bProcessing": true,
                "bServerSide": true,
                "sScrollX": true,
                "bAutoWidth":false,
                "fnServerParams": function (aoData) {
                    var classSelect = $("#classSelect").val();
                    var datemin = $('#startTime').val();
                    var datemax = $("#endTime").val();
                    var shopName = $("#shopName").val();
                    var managerName = $("#managerName").val();
                    aoData.push(
                        { "name": "dateMin", "value": datemin },
                        { "name": "dateMax", "value": datemax },
                        { "name": "shopName", "value": shopName },
                        { "name": "managerName", "value": managerName },
                        { "name": "status", "value": classSelect }
                    );
                },
                "sAjaxSource": "/Shop/GetShopsByPage",
                "aoColumns": [
                    { "mDataProp": 'id' },
                    { "mDataProp": 'name' },
                    { "mDataProp": 'company_name' },
                    {
                        "mDataProp": 'mangername', "mRender": function (data, type, full) {
                            return data+"</br>"+full.mangermobile;
                        }
                    },
                    {
                        "mDataProp": 'start_date', "mRender": function (data, type, full) {
                            return (data == null ? "" : moment(data).format("YYYY-MM-DD")) + " - " + (full.end_date == null ? "" : moment(full.end_date)).format("YYYY-MM-DD");
                        }
                    },
                    { "mDataProp": 'account_period' },
                    {
                        "mDataProp": 'add_time', "mRender": function (data, type, full) {
                            return moment(data).format("YYYY-MM-DD HH:mm:ss");
                        }
                    },
                    {
                        "mDataProp": 'status', "mRender": function (data, type, full) {
                            if (data == 1) {
                                return '<a style="text-decoration:none" class="btn btn-success" onClick="shop_noPass(this,' + full.id + ')" href="javascript:;" title="点击禁用">启用</a>';
                            } else {
                                return '<a style="text-decoration:none" class="btn btn-danger" onClick="shop_pass(this,' + full.id + ')" href="javascript:;" title="点击启用">禁用</a>';
                            }
                        }
                    },
                    {
                        "mDataProp": 'id', "mRender": function (data, type, full) {
                            return full.status == 1 ? ('<a class="btn btn-default " href = "/shop/editStore/' + full.id + '"  title="编辑">编辑</a> '
                                + '<a class="btn btn-default " href = "/shop/SetStoreCategory/' + full.id + '"  title="设置店铺商品类别">管理商品类别</a> '
                                + '<a class="btn btn-info "  href="/shop/SelectGoodsCategory/' + full.id + '" title="添加商品">添加商品</a> ') : '';
                        }
                    }
                ]
            });
        }

        /*启用*/
        function shop_pass(obj, id) {
            layer.confirm('确定启用店铺？', function (index) {
                $.ajax({
                    type: "post",
                    url: "/shop/SetShopIsUse",
                    dataType: "json",
                    data: { id: id, isUse: 1 },
                    success: function (data) {
                        if (data.status == "y") {
                            $(obj).text("启用").attr("onClick", "shop_noPass(this," + id + ")").attr("title", "点击启用").attr("class", "btn btn-success");
                            layer.msg('已启用', { icon: 6, time: 1000 }, function () {
                                window.location.href = "/shop/store";
                            });
                        }
                        else {
                            alert(data.info);
                        }
                    }
                });

            });
        }
        /*禁用*/
        function shop_noPass(obj, id) {
            layer.confirm('确定禁用店铺？', function (index) {
                $.ajax({
                    type: "post",
                    url: "/shop/SetShopIsUse",
                    dataType: "json",
                    data: { id: id, isUse: 0 },
                    success: function (data) {
                        if (data.status == "y") {
                            $(obj).text("禁用").attr("onClick", "shop_pass(this," + id + ")").attr("title", "点击启用").attr("class", "btn btn-danger");
                            layer.msg('已禁用', { icon: 5, time: 1000 }, function () {
                                window.location.href = "/shop/store";
                            });
                        }
                        else {
                            alert(data.info);
                        }
                    }
                });

            });
        }
    </script>
}
